package com.common.android.ui.LocalAlbum;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

import android.os.Environment;
import android.os.Parcel;
import android.os.Parcelable;

public class SortedMediaFiles implements Parcelable{
	public FileInfo mDirInfo;
	private PriorityQueue<FileInfo> mFiles;

	public SortedMediaFiles(){
		mFiles = new PriorityQueue<FileInfo>(11,
			new Comparator<FileInfo>() {	
				@Override
				public int compare(FileInfo lhs, FileInfo rhs) {
					return lhs.lastModifiedCmp(rhs);
				}
			});
	}

	public void addFile(FileInfo fi) {
		mFiles.add(fi);
	}

	public boolean isFilesEmpty() {
		return mFiles.isEmpty();
	}

	public ArrayList<FileInfo> getFilesList() {
		ArrayList<FileInfo> list = new ArrayList<FileInfo>();
		FileInfo fi = mFiles.poll();
		while (fi != null) {
			list.add(fi);
			fi = mFiles.poll();
		}
		for (FileInfo temp : list) {
			mFiles.add(temp);
		}
		return list;
	}
	
	public boolean isInCameraPath(){
		if(null == mDirInfo){
			return false;
		}
				
		String strCameraPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM";
		return mDirInfo.mFilePathOrName.equalsIgnoreCase(strCameraPath);
	}

	/**
	 * 判断是否是相册路径，用于明星App
	 * 
	 * <br>
	 * Created 2014-5-12 下午9:43:16
	 * 
	 * @return true：是；false：不是
	 * @author : HuangYK
	 */
	public boolean isTheCameraPath() {
		if (null == mDirInfo) {
			return false;
		}

		String strCameraPath = Environment.getExternalStorageDirectory()
				.getAbsolutePath() + "/DCIM/Camera";
		return mDirInfo.mFilePathOrName.equals(strCameraPath);
	}
	
	//移除图片，相机目录专用
	public void removeFile(String strFile) {
		if(mFiles.isEmpty()){
			return;
		}
		
		List<FileInfo> list = new ArrayList<FileInfo>();
		FileInfo fi = mFiles.poll();
		while (fi != null) {
			if(!fi.mFilePathOrName.equalsIgnoreCase(strFile)){
				list.add(fi);
			}			
			fi = mFiles.poll();
		}
		for (FileInfo temp : list) {
			mFiles.add(temp);
		}
	}
	
	//取得路径文件夹名
	public String getDirName(){
		if(null == mDirInfo){
			return "";
		}
		
		//移除末尾 '/' 
		String strRet = new String(mDirInfo.mFilePathOrName);
		if(strRet.endsWith("/")) {
			strRet = strRet.substring(0, strRet.length());
		}
		
		int iPos = strRet.lastIndexOf('/');
		return strRet.substring(iPos + 1, strRet.length());
	}
	
	//取得文件数目
	public int getFilesCount(){
		if(null == mFiles){
			return 0;
		}
		
		return mFiles.size();
	}
	
	//获取第一张图片的路径
	public FileInfo getFirstFile(){
		if(null == mFiles){
			return null;
		}
		
		FileInfo fi = mFiles.peek();
		if(null == fi){
			return null;
		}
		
		return fi;
	}

	private SortedMediaFiles(Parcel in) {
		mDirInfo = in.readParcelable(FileInfo.class.getClassLoader());
		
		mFiles = new PriorityQueue<FileInfo>(11,
				new Comparator<FileInfo>() {	
					@Override
					public int compare(FileInfo lhs, FileInfo rhs) {
						return lhs.lastModifiedCmp(rhs);
					}
				});
		
		int iSize = in.readInt();
		
		for(int i = 0; i < iSize; i++){
			mFiles.add((FileInfo) (in.readParcelable(FileInfo.class.getClassLoader())));
		}
	}
	
	public static final Parcelable.Creator<SortedMediaFiles> CREATOR = new Parcelable.Creator<SortedMediaFiles>() {
		public SortedMediaFiles createFromParcel(Parcel in) {
			return new SortedMediaFiles(in);
		}

		public SortedMediaFiles[] newArray(int size) {
			return new SortedMediaFiles[size];
		}
	};
	
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		//先写入文件夹名
		dest.writeParcelable(mDirInfo, flags);
		
		List<FileInfo> listFI = getFilesList();		
		if (null != listFI) {
			//写入文件个数
			dest.writeInt(listFI.size());
			
			for (FileInfo fi : listFI) {
				dest.writeParcelable(fi, flags);
			}
		}else{
			dest.writeInt(0);
		}
	}

}
